/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.service.utils.scheduled;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimerTask;
import org.cx1.model.utils.EmailInvitee;
import org.cx1.model.utils.ServiceResponse;
import org.cx1.service.api.IEmailService;
import org.cx1.service.api.IInviteService;
import org.cx1.service.impl.DInviteService;
import org.cx1.service.impl.MEmailService;
import org.cx1.service.impl.MInviteService;

/**
 *
 * @author ECH
 */
public class TKSendEmailInvite extends TimerTask {

    private IInviteService dInviteService = new DInviteService();    
    private IEmailService emailService = new MEmailService();
    private IInviteService inviteService = new MInviteService();    
    private Date lastRun = new Date();
    
    @Override
    public void run() {
        try {
            Date now = new Date();
            Calendar cal = Calendar.getInstance();
            cal.setTime(now);               
            int hour = cal.get(Calendar.HOUR_OF_DAY); 
             
            ServiceResponse<Boolean> response1 = dInviteService.deleteExisitingUsersInEmailInviteList();
            ServiceResponse<List<EmailInvitee>> response = dInviteService.readEmailInviteesByProcessed(0, 100, false);
            List<EmailInvitee> emailInvitees = response.getResponse();

            if (emailInvitees != null && emailInvitees.size() > 0) {
                for (EmailInvitee invitee : emailInvitees) {
                    emailService.sendInvite1("", invitee.getEmail());      
                    dInviteService.setEmailInviteeProcessed(invitee.getEmail(), true);
                }               
                //CX: email check
                emailService.sendInvite1("", "chux18@gmail.com");   
            }                
            lastRun = now;
        } 
        catch (Exception ex) {   
        
        }
        finally {   
            
        }
    }
        
    public Date getLastRun() {
        return lastRun;
    }

    public void setLastRun(Date lastRun) {
        this.lastRun = lastRun;
    }

    public static void main(String argv) {
        TKSendEmailInvite tk = new TKSendEmailInvite();
        tk.run();
    }
}
